{% extends 'base.html' %}
{% block title %}登录{% endblock %}
{% block head %}
	<link rel="stylesheet"  href="{{ url_for('static',filename='css/login.css')}}"/>
{% endblock %}
{% block main %}
<script>
		//检查手机号
		function checkPhoneNum(){
			var phoneNum = $.trim($('#telephoneInput').val());
			$('#telephoneInput').val(phoneNum);
			if(phoneNum==""){
			$('#telephoneInputTip').text("请输入手机号");
			$('#telephoneInput').focus();
			return false;
			} else {
				var rs1=/^0\d{2,3}-\d{5,9}$/;
				var rs2=/^1[34578]\d{9}$/;
				if(rs1.test(phoneNum) || rs2.test(phoneNum)){
					var flag = false
					$.ajax({
							type:"post",
							async:false,
							dataType: "json",//从服务端返回的数据类型
							url:"/register/checkTel?tel="+phoneNum,
							success:function(data){
								if(data.status==501){
									// $('#telTip').text("该手机号还未注册，请先注册");
									// alert(data.status)
									$('#telTip').html("该手机号还未注册，请先注册<a text-align='center' href='{{url_for('register')}}'>去注册</a>")
									$('#telTip').focus();
									flag =false;

								}else{
									$('#telTip').text("");
									flag =true;

								}
							},
							error:function(data){
								$('#telTip').text("网络异常");
								$('#telTip').focus();
								flag =false;
							}

						});
					
					return flag;
					
				}else{
					$('#telephoneInputTip').text("输入的手机号无效");
					$('#telephoneInput').focus();
					return false;
				}
			}
			
				
			}

	function onLogin(){
		if(!checkPhoneNum()){
			return
		}
		$.ajax({
      //几个参数需要注意一下
        type: "POST",//方法类型
        dataType: "json",//从服务端返回的数据类型
        url: "/login" ,//url
        data: $('#loginform').serialize(),
        success: function (result) {
			// alert(result.message);//打印服务端返回的数据(调试用)
			if(result.status== 501){
				window.location.href="/";
			}
			else{
				$('#tip').text(result.message);
			}
        },
        error : function(result) {
			// $("#transferStauts").text(result.responseText);
			alert(result.responseText)
        }
      });
	}

</script>
<div class="login">
	<h3 class='page-title'>登录</h3>
	<form id="loginform">
		<div class="form-container ">
			<div class="form-group">
				<input id ="telephoneInput" type="text" class="form-control"  placeholder="手机号" name="telephone" onchange="checkPhoneNum()">
				<span id="telTip"></span>
			</div>
			<div class="form-group">
				<input type="password" class="form-control"  placeholder="密码" name="password">
				<span id="tip"></span>
			</div>
			
			<div class="form-group">
				<button type="button" class="btn btn-primary btn-block" onclick="onLogin()">登录</button>
			</div>
			
		</div>
    </form>
</div>
{% endblock %}